#include "lapi_log.h"

#include <cutils/properties.h>

#include "utils/Log.h"
#undef LOG_TAG
#define LOG_TAG "LAPI_SRV"

static FILE* g_log_fp = NULL;
static int g_log_level = -1;

int g_debug_mode = 0;

/*char *lapi_log_dir()
{
    memset(str_log_dir, 0, FILE_PATH_LEN);
    if (getenv("OMS_LOG_PATH") != NULL)
        strcpy(str_log_dir, getenv("OMS_LOG_PATH"));
    else
        strcpy(str_log_dir, "/data");
    strcat(str_log_dir, "/location");
    return str_log_dir;
}

char *lapi_log_file()
{
    memset(str_log_file, 0, FILE_PATH_LEN);
    strcpy(str_log_file, lapi_log_dir());
    strcat(str_log_file, "/lapi_log.txt");
    return str_log_file;
}

char *lapi_log_file_prev()
{
    memset(str_log_file_prev, 0, FILE_PATH_LEN);
    strcpy(str_log_file_prev, lapi_log_dir());
    strcat(str_log_file_prev, "/lapi_log_prev.txt");
    return str_log_file_prev;
}

char *detailed_log() 
{
    memset(str_detailed_log, 0, FILE_PATH_LEN);
    strcpy(str_detailed_log, lapi_log_dir());
    strcat(str_detailed_log, "/detailed_log.txt");
    return str_detailed_log;
}

char *brief_log()
{
    memset(str_brief_log, 0, FILE_PATH_LEN);
    strcpy(str_brief_log, lapi_log_dir());
    strcat(str_brief_log, "/brief_log.txt");
    return str_brief_log;
}

char *agps_log()
{
    memset(str_agps_log, 0, FILE_PATH_LEN);
    strcpy(str_agps_log, lapi_log_dir());
    strcat(str_agps_log, "/agps_log.txt");
    return str_agps_log;
}

char *agps_log_filename()
{
    memset(str_agps_log_filename, 0, FILE_PATH_LEN);
    strcpy(str_agps_log_filename, lapi_log_dir());
    strcat(str_agps_log_filename, "/agpsLog.loc");
    return str_agps_log_filename;
}

char *previous_location_filename()
{
    memset(str_previous_location_filename, 0, FILE_PATH_LEN);
    strcpy(str_previous_location_filename, lapi_log_dir());
    strcat(str_previous_location_filename, "/prevPositionLog.loc");
    return str_previous_location_filename;
}

char *brief_log_filename()
{
    memset(str_brief_log_filename, 0, FILE_PATH_LEN);
    strcpy(str_brief_log_filename, lapi_log_dir());
    strcat(str_brief_log_filename, "/briefLog.txt");
    return str_brief_log_filename;
}

char *detailed_log_filename()
{
    memset(str_detailed_log_filename, 0, FILE_PATH_LEN);
    strcpy(str_detailed_log_filename, lapi_log_dir());
    strcat(str_detailed_log_filename, "/detailedLog.txt");
    return str_detailed_log_filename;
}

char *slc_log_filename()
{
    memset(str_slc_log_filename, 0, FILE_PATH_LEN);
    strcpy(str_slc_log_filename, lapi_log_dir());
    strcat(str_slc_log_filename, "/slcLog.txt");
    return str_slc_log_filename;
}*/

int lapi_log_start(void)
{
    struct stat buf;
    char cmd[80];
    char debug[8];

    //change directory "/data/location" mode so that JAVA app can access it
    chmod(LAPI_CFG_PATH, 0777);

    memset(debug, 0, 8);
    property_get("sys.gps.debug", debug, NULL);
    LOGD("sys.gps.debug = %s", debug);

    if (!strcmp(debug, "false") || !strcmp(debug, "")) {
        LOGI("Debug mode off, no log output.");
        g_debug_mode = 0;
        sprintf(cmd, "rm -r %s", LAPI_LOG_PATH);
        system(cmd);
        return -1;
    }

    g_debug_mode = 1;

    if (lstat(LAPI_LOG_PATH, &buf) < 0) {
        mkdir(LAPI_LOG_PATH, 0777);
    } else {
        if (!S_ISDIR(buf.st_mode)) {
            unlink(LAPI_LOG_PATH);
            mkdir(LAPI_LOG_PATH, 0777);
        }
    }

    // backup previous log
    sprintf(cmd, "mv %s %s~", LAPI_LOG_FILE, LAPI_LOG_FILE);
    system(cmd);

    g_log_fp = fopen(LAPI_LOG_FILE, "w+");
    if (g_log_fp == NULL)
        return -1;

    /* get log level here */
    if (getenv("TELEPHONY_LOG_LEVEL") != NULL)
        g_log_level = strtol(getenv("TELEPHONY_LOG_LEVEL"), NULL, 10);
    else
        g_log_level = LOG_LEVEL_VERBOSE;

    return 0;
}

void lapi_log_stop(void)
{
    if (!g_debug_mode)
        return;

    fflush(g_log_fp);
    fclose(g_log_fp);
}

static void genTS(char *buff, int buff_size)
{
    time_t     now;
    struct tm  *ts;

    // Get the current time
    time(&now);

    // Format and print the time, "yyyy-mm-dd hh:mm:ss"
    ts = localtime(&now);
    strftime(buff, buff_size, "%Y-%m-%d %H:%M:%S", ts);
}

void lapi_log(int lev, const char *fmt, ...)
{
    long offset;
    va_list ap;
    char ts_buf[80];

    if (!g_debug_mode) {
        /*va_start(ap, fmt);
        LOGD(fmt, ap);
        va_end(ap);*/
    } else {
        if ((g_log_level == LOG_LEVEL_DISABLED) || (lev > g_log_level))
            return;

        memset(ts_buf, 0, 80);
        //fprintf(g_log_fp, "[%ld] ", time(NULL));
        genTS(ts_buf, 80);
        fprintf(g_log_fp, "[%s] ", ts_buf);

        va_start(ap, fmt);
        vfprintf(g_log_fp, fmt, ap);
        fflush(g_log_fp);
        va_end(ap);

        /* max length checking */
        if ((offset = ftell(g_log_fp)) < 0)
            printf("Fail to lseek to get current offset, errno is: %d, %s ", errno, strerror(errno));

        if (offset >= LAPI_LOG_FILE_MAX_LEN) {
            if (fseek(g_log_fp, 0, SEEK_SET) < 0)
                printf("Fail to rewind, errno is: %d, %s", errno, strerror(errno));
        }
    }
}

void log_dispatcher(LAPI_LOG_LEV lev, const char *fmt, ...)
{
    long offset;
    va_list ap;
    char new_fmt[1024];
    char ts_buf[80];

    if (!g_debug_mode) {
        /*va_start(ap, fmt);
        LOGI(new_fmt, ap);
        va_end(ap);*/
    } else {
        if ((g_log_level == LOG_LEVEL_DISABLED) || (lev > g_log_level))
            return;

        memset(new_fmt, 0, 1024);

        switch (lev) {
            case LOG_LEVEL_DEBUG:
                sprintf(new_fmt, "[LAPI DBG] %s\n", fmt);
                break;
            case LOG_LEVEL_INFO:
                sprintf(new_fmt, "[LAPI INF] %s\n", fmt);
                break;
            case LOG_LEVEL_ERROR:
                sprintf(new_fmt, "[LAPI ERR] %s\n", fmt);
                break;
            default:
                return;
        }

        memset(ts_buf, 0, 80);
        //fprintf(g_log_fp, "[%ld] ", time(NULL));
        genTS(ts_buf, 80);
        fprintf(g_log_fp, "[%s] ", ts_buf);

        va_start(ap, fmt);
        vfprintf(g_log_fp, new_fmt, ap);
        fflush(g_log_fp);
        va_end(ap);

        /* max length checking */
        if ((offset = ftell(g_log_fp)) < 0)
            printf("Fail to lseek to get current offset, errno is: %d, %s ", errno, strerror(errno));

        if (offset >= LAPI_LOG_FILE_MAX_LEN) {
            if (fseek(g_log_fp, 0, SEEK_SET) < 0)
                printf("Fail to rewind, errno is: %d, %s", errno, strerror(errno));
        }
    }
}
